package com.scau.meetingbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau.meetingbook.dto.CancellationRequestDTO;
import com.scau.meetingbook.mapper.MeetingRoomsMapper;
import com.scau.meetingbook.mapper.RoomReservationsMapper;
import com.scau.meetingbook.pojo.CancellationRequests;
import com.scau.meetingbook.mapper.CancellationRequestsMapper;
import com.scau.meetingbook.pojo.MeetingRooms;
import com.scau.meetingbook.pojo.RoomReservations;
import com.scau.meetingbook.service.CancellationRequestsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.meetingbook.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CancellationRequestsServiceImpl extends ServiceImpl<CancellationRequestsMapper, CancellationRequests> implements CancellationRequestsService {

    @Autowired
    private CancellationRequestsMapper cancellationRequestsMapper;

    @Autowired
    private RoomReservationsMapper roomReservationsMapper;

    @Autowired
    private MeetingRoomsMapper meetingRoomsMapper;

    @Override
    public ResultVO insertCancellationRequests(CancellationRequests cancellationRequests) throws Exception {
        cancellationRequestsMapper.insert(cancellationRequests);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO<CancellationRequests> getCancellationRequestsById(Integer id) throws Exception {
        return ResultVO.success(null, cancellationRequestsMapper.selectById(id));
    }

    @Override
    public ResultVO updateCancellationRequestsById(CancellationRequests cancellationRequests) throws Exception {
        cancellationRequestsMapper.updateById(cancellationRequests);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO deleteCancellationRequestsById(Integer id) throws Exception {
        cancellationRequestsMapper.deleteById(id);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO createCancellationRequests(CancellationRequests cancellationRequests) {

        cancellationRequests.setStatus("pending");
        cancellationRequests.setRequestTime(new Date());

        RoomReservations reservation = roomReservationsMapper.selectOne(new QueryWrapper<RoomReservations>().eq("reservation_id", cancellationRequests.getReservationId()));
        Double hours = reservation.getDurationHours();

        //判断取消的订单是否需要退款
        if(reservation.getReservationStatus().equals("confirmed")){
            MeetingRooms roomId = meetingRoomsMapper.selectOne(new QueryWrapper<MeetingRooms>().eq("room_id", reservation.getRoomId()));
            BigDecimal pay = roomId.getHourlyRentalPrice().multiply(BigDecimal.valueOf(hours));

            // 计算24小时后的时间点
            Date twentyFourHoursAgo = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
            // 计算48小时后的时间点
            Date fortyEightHoursAgo = new Date(System.currentTimeMillis() + 48 * 60 * 60 * 1000);
            // 计算72小时后的时间点
            Date seventyTwoHoursAgo = new Date(System.currentTimeMillis() + 72 * 60 * 60 * 1000);

            //计算退款金额
            if(reservation.getFullStartDateTime().after(seventyTwoHoursAgo)){
                cancellationRequests.setRefundAmount(pay);
            }else if(reservation.getFullStartDateTime().after(fortyEightHoursAgo)){
                cancellationRequests.setRefundAmount(pay.multiply(BigDecimal.valueOf(0.75)));
            }else if(reservation.getFullStartDateTime().after(twentyFourHoursAgo)){
                cancellationRequests.setRefundAmount(pay.multiply(BigDecimal.valueOf(0.25)));
            }else{
                throw new RuntimeException("取消申请失败");
            }
        }else{
            cancellationRequests.setRefundAmount(BigDecimal.ZERO);
        }

        cancellationRequestsMapper.insert(cancellationRequests);

        return ResultVO.success(null);
    }

    @Transactional
    @Override
    public ResultVO auditCancellationRequests(Integer requestId) {
        CancellationRequests request = cancellationRequestsMapper.selectById(requestId);
        if(request.getStatus().equals("pending")){
            request.setStatus("approved");
            cancellationRequestsMapper.updateById(request);
            RoomReservations reservation = roomReservationsMapper.selectOne(new QueryWrapper<RoomReservations>().eq("reservation_id", request.getReservationId()));
            reservation.setReservationStatus("cancelled");
            roomReservationsMapper.updateById(reservation);
            //  TODO 退款

            return ResultVO.success(null);
        }else{
            return ResultVO.fail("申请已处理");
        }
    }

    @Override
    public ResultVO rejectCancellationRequests(Integer requestId) {
        CancellationRequests request = cancellationRequestsMapper.selectById(requestId);
        if(request.getStatus().equals("pending")){
            request.setStatus("rejected");
            cancellationRequestsMapper.updateById(request);
            return ResultVO.success(null);
        }else{
            return ResultVO.fail("申请已处理");
        }
    }

    @Override
    public ResultVO<List<CancellationRequestDTO>> getAllCancellationRequests() {
        // 使用自定义Mapper查询DTO数据
        List<CancellationRequestDTO> dtoList = cancellationRequestsMapper.selectCancellationRequestDTOs();
        return ResultVO.success("成功", dtoList);
    }
}